import 'dart:developer';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import '../providerDemo/counter_provider.dart';
import '../providerDemo/second_counter_page.dart';

class SelectorFirstPage extends StatefulWidget {
  @override
  _SelectorFirstPageState createState() => _SelectorFirstPageState();
}

class _SelectorFirstPageState extends State<SelectorFirstPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("第一个页面"),
      ),
      body: Center(
        child: Column(
          children: [
            Consumer<CounterProvider>(builder: (ctx, counterPro, child) {
              return Text(
                "当前计数：${counterPro.counter}",
                style: TextStyle(fontSize: 20, color: Colors.red),
              );
            }),
            // Consumer<CounterProvider>(builder: (ctx, counterPro, child) {
            //   return RaisedButton(
            //       child: Text("add"),
            //       onPressed: () {
            //         counterPro.counter += 1;
            //       });
            // }),

            Selector<CounterProvider, CounterProvider>(
                selector: (ctx, provider) => provider,
                shouldRebuild: (pre, next) => false,
                builder: (ctx, counterPro, child) {
                  print("selector--RaisedButton展示的位置builder被调用");
                  return RaisedButton(
                      child: Text("selectorAdd"),
                      onPressed: () {
                        counterPro.counter += 1;
                      });
                }),

            SizedBox(
              height: 10,
            ),
            RaisedButton(
              child: Text('SecondPage'),
              onPressed: pushToSecondPage,
            ),
          ],
        ),
      ),
    );
  }

  void pushToSecondPage() {
    Navigator.push(context, MaterialPageRoute(builder: (context) {
      return SecondPage();
    }));
  }
}
